# encoding = utf8

import os
from pathlib import Path

root = r'K:\GIS\Papers\P005SWATBLJ\swatblj\data\weather20052019\中国地面气候资料日值数据集（V3.0）-201001201912\weather'
ind = ['PRE-13011', 'RHU-13003', 'TEM-12001', 'WIN-11002']  # 气象指标名称
stations = [56079, 56080, 56093, 56096, 56182, 57014, 57106, 57206]  # 气象站编号
monthdays = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}  # 逐月日期对照字典


for indi in ind:
    for station in stations:
        txtfile = open('{}{}.txt'.format(indi[0:3], station), 'w')  # 创建SWAT格式的文本文件
        txtfile.write('20100101\n')  # 先填个表头，第一个日期
        for year in range(2010, 2019):  # 循环2010-2019年
            for month in range(1, 13):  # 循环1-12月
                filename = '{}\SURF_CLI_CHN_MUL_DAY-{}-{}{:02d}.TXT'.format(root, indi, year, month)
                if Path(filename).exists():  # 判断下要读取的天气文件是否存在
                    file = open(filename, 'r')  # 文件存在，打开它
                    alllines = file.readlines()  # 读取该文本的所有行为列表，每一行为列表中一个元素
                    file.close()
                    line_number = 0
                    for line in alllines:    # 循环处理每一行
                        lineinfo = line.split()   # 把每一行按空格拆分成列表
                        if lineinfo[0] == str(station):
                            line_number = line_number + 1
                            if indi[0:3] == 'PRE':
                                pre_data = eval(lineinfo[9]) * 0.1  # 降水*0.1
                                if pre_data >= 3000:
                                    txtfile.write('0.1\n')  # 如果是32700 微量 或者缺测，填上个0.1
                                else:
                                    txtfile.write('{:.1f}\n'.format(pre_data))
                            elif indi[0:3] == 'RHU':
                                rhu_data = eval(lineinfo[7])
                                if rhu_data >= 3000:
                                    txtfile.write('-99\n')  # 如果空气相对湿度大于3000，标为缺测-99
                                else:
                                    txtfile.write('{}\n'.format(lineinfo[7]))
                            elif indi[0:3] == 'TEM':
                                txtfile.write('{:.1f},{:.1f}\n'.format(eval(lineinfo[8]) * 0.1,
                                                                       eval(lineinfo[9]) * 0.1))  # 气温*0.1
                            elif indi[0:3] == 'WIN':
                                txtfile.write('{:.1f}\n'.format(eval(lineinfo[7]) * 0.1))  # 风速*0.1
                            else:
                                print('i dont know what happend')
                    if line_number == 0:
                        print('{}-{}-{}-{} 这个月没有数据，填上空值-99'.format(indi, station, year, month))
                        for i in range(monthdays[month]):
                            txtfile.write('-99\n')
                        if (year % 4 == 0) and (year % 100 != 0) or (year % 400) == 0:  # 闰年2月的没有数据的，最后面多加一行29号的
                            if month == 2:
                                txtfile.write('-99\n')
                    elif line_number < monthdays[month]:
                        print('{}-{}-{}-{} 这个月有数据，但存在一些日期的缺失？？？？？？？'.format(indi, station, year, month))
                    else:
                        print('{}-{}-{}-{} 这个月输出了{}天的数据'.format(indi, station, year, month, line_number))

                else:
                    print('{} does"t exist'.format(filename))
        txtfile.close()
        print('---------------------------')
        print('---------------------------')
        print('---------------------------')
        print('{}{}.txt输出完成'.format(indi[0:3], station))
        print('---------------------------')
        print('---------------------------')
        print('---------------------------')
print('全部输出完成')
